package com.dollar.producer_consumer;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) {
        FixedAmountFlush<String> fixedAmountFlush = new FixedAmountFlush<>(10);
        //生产者线程
        for (int i = 0; i < 1; i++) {
            new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    String s = j + "";
                    //System.out.println(Thread.currentThread().getName() + " produce: " + s);
                    fixedAmountFlush.put(s);
                }
            }, "producer-" + i).start();
        }
        //消费者线程
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                while (true) {
                    List<String> some = fixedAmountFlush.takeSome(5);
                    try {
                        //模拟刷盘
                        TimeUnit.MILLISECONDS.sleep(200);
                        System.out.println(Thread.currentThread().getName() + " consume: " + some);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "consumer-" + i).start();
        }
    }
}
